package com.wgc.controller;

import java.io.IOException;
import java.math.BigInteger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.crypto.CipherException;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.admin.Admin;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.utils.Convert;

import com.wgc.compile.Coin;

/**
 * 使用web3j-java 生成合约java代码，进行部署调用
 * @author wwings
 *
 */
@RestController
public class ContractController {
	
	@Autowired
	Admin Admin;
	
	@Autowired
	Web3j web3j;
	@RequestMapping("/deployContract")
	public void deploy() throws IOException, CipherException {
		Credentials credentials = WalletUtils.loadCredentials("", "E:\\ethereum\\data\\keystore\\UTC--2018-10-25T02-43-00.388929300Z--041ccb7d152f1db2d6a935360ad5cb7b542d6e5f");
		//可以根据私钥生成
		RemoteCall<Coin> deploy = Coin.deploy(web3j, credentials, BigInteger.valueOf(100), BigInteger.valueOf(3041592));
		/*RemoteCall<TokenERC20> deploy = Coin.deploy(web3j, credentials,
				Convert.toWei("10", Convert.Unit.GWEI).toBigInteger(),
				BigInteger.valueOf(3000000),
				BigInteger.valueOf(5201314),
				"0xF287bF936F5173beeA2Be2c1d26837d0353B47E3", "mt");*/
		try {
			Coin coin = deploy.send();	
			coin.isValid();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@RequestMapping("/userContract")
	public void use() throws IOException, CipherException {
		String contractAddress = "0x5cee50b7ace77646e44da2d455d2041e284c8483";
		Credentials credentials = WalletUtils.loadCredentials("", "E:\\ethereum\\data\\keystore\\UTC--2018-10-25T02-43-00.388929300Z--041ccb7d152f1db2d6a935360ad5cb7b542d6e5f");//可以根据私钥生成
		Coin contract = Coin.load(contractAddress, web3j, credentials,
				Convert.toWei("10", Convert.Unit.GWEI).toBigInteger(),
				BigInteger.valueOf(100000));
		String myAddress = "0x041CCb7d152F1dB2d6A935360ad5cb7B542d6E5f";
		String toAddress = "0x508C5270500361574AdEA14C5A3B545Ed514da57";
		BigInteger amount = BigInteger.ONE;
		try {
			Uint256 result = contract.balances(new Address(myAddress)).send();
			System.out.println("*************"+ result.getValue());
			TransactionReceipt receipt = contract.mint(new Address(toAddress), new Uint256(new BigInteger("10"))).send();
			System.out.println("transaction hash ...." + receipt.getTransactionHash());;
			//调用合约
			//BigInteger balance = contract.balanceOf(myAddress).send();
			//TransactionReceipt receipt = contract.transfer(toAddress, amount).send();
			//etc..
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*public static createCredentials () {
		ECKeyPair ecKeyPair = new ECKeyPair(privateKey, publicKey)
		Credentials credentials = new Credentials(null, null);
		
	}*/
}
